package com.zzb.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.ProductVO;
import com.zzb.service.ProductService;
import com.zzb.service.impl.ProductServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //得到前端数据
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String content = req.getParameter("content");
        String enable = req.getParameter("enable");
        String isShow = req.getParameter("isShow");
        String remark = req.getParameter("remark");
        String typeId = req.getParameter("typeId");
        String marketPrice = req.getParameter("marketPrice");
        String imgHref = req.getParameter("imgHref");



        //对前端数据进行封装
        ProductVO productVO = ProductVO.builder()
                .name(Tool.nullToStr(name))
                .enable(Integer.valueOf(enable))
                .isShow(Integer.valueOf(isShow))
                .content(Tool.nullToStr(content))
                .normalPrice(Tool.strToBigDecimal(normalPrice))
                .marketPrice(Tool.strToBigDecimal(marketPrice))
                .remark(Tool.nullToStr(remark))
                .typeId(Integer.valueOf(typeId))
                .imgHref(Tool.nullToStr(imgHref))
                .build();

        //调用服务逻辑
        ProductService productService = new ProductServiceImpl();
        //得到响应数据
        ResultDTO resultDTO = productService.add(productVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
